<template>
  <a-modal
    title="产品列表"
    :width="1200"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :footer="null"
    @ok="handleSubmit"
    @cancel="handleCancel">
    <a-card :bordered="false">

      <s-table
        ref="table"
        size="default"
        :columns="columns"
        :data="loadData"
        :alert="true"
        :rowKey="(record) => record.code"
        :rowSelection="null"
        :showPagination="false"
      >
          <span slot="remark" slot-scope="text, record">
            <a-tooltip placement="topLeft" :title="record.material.remark">
              {{record.material.remark}}
            </a-tooltip>
          </span>
      </s-table>

    </a-card>
  </a-modal>
</template>

<script>
import {STable} from '@/components'
import {page} from '@/api/modular/main/emsordermaterial/emsOrderMaterialManage'
import {handleProductionInStoreOrderProductInStore, handleProductionOrderProductComplete} from '@/api/modular/main/emsorder/emsOrderManage'

export default {
  components: {
    STable,
  },

  data: function () {
    return {
      title: null,
      advanced: false, // 高级搜索 展开/关闭
      queryParam: {},  // 查询参数
      columns: [
        {
          title: '产品编号',
          dataIndex: 'materialCode'
        },
        {
          title: '产品名称',
          dataIndex: 'materialName'
        },
        {
          title: '入库数量',
          dataIndex: 'completedNumber',
        },
        {
          title: '库位',
          dataIndex: 'material.orgIdStorehousePositionName',
        },
        {
          title: '备注',
          dataIndex: 'remark',
          ellipsis: true,
          scopedSlots: {customRender: 'remark'}
        },
        {
          title: '库位备注',
          dataIndex: 'material.storehousePositionRemark',
        },
        {
          title: '入库人员',
          dataIndex: 'updateUserName'
        },
        {
          title: '入库时间',
          dataIndex: 'updateTime'
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return page(Object.assign(parameter, this.queryParam)).then((res) => {
          return res.data
        })
      },
      selectedRowKeys: [],
      selectedRows: [],
      defaultExpandedKeys: [],
      visible: false,
      confirmLoading: false,
      emsOrder: null,
      viewState: '',
      extra: {},
    }
  },
  computed: {},
  methods: {
    show(record) {
      this.visible = true
      this.emsOrder = record
      this.queryParam = {}
      this.queryParam.orderId = record.orderId
      this.queryParam.pageSize = -1

      setTimeout(() => {
        this.handleOk()
        this.$refs.table.clearSelected()
      }, 100)
    },
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    handleOk() {
      this.$refs.table.refresh()
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    handleSubmit() {
      this.handleInStore(this.selectedRows)
    },
    handleCancel() {
      this.visible = false
    },
    handleRowSubmit(record) {
      this.handleInStore([record])
    },
    handleInStore(rows) {
      if (rows.length == 0) {
        this.$message.error('请选择后再操作')
        return
      }
      let param = Object.assign({}, this.emsOrder, {orderMaterials: rows})
      handleProductionInStoreOrderProductInStore(param).then(res => {
        this.visible = false
        this.handleOk()
        this.$emit('ok')
      })
    },
  }
}
</script>

<style lang="less">
.table-operator {
  margin-bottom: 18px;
}

button {
  margin-right: 8px;
}
</style>
